package com.aifeng.hassan_food.sale

import android.annotation.SuppressLint
import android.os.Bundle
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.formatDecimal
import com.aifeng.common_ui.helper.getString
import com.aifeng.common_ui.helper.optNotNull
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.OrderViewModel
import kotlinx.android.synthetic.main.activity_history_detail.*
import org.json.JSONObject
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HistoryDetailActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history_detail)
        initTitle(title = "详情", divider = false)

        getData()
    }

    override fun getViewModel() = getViewModel<OrderViewModel>()

    @SuppressLint("SetTextI18n")
    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        history_hint.text = "${intent.getExtra("hint")}数据"
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().historyDetail(
            intent.getExtra("date"),
            getString("userId")
        )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByNoError(
                event = getViewModel(),
                onSuccess = {
                    val obj = JSONObject(it)
                    history_total1.text = "${obj.optNotNull("salePerson").formatDecimal()}元"
                    history_total2.text = "${obj.optNotNull("saleTeam").formatDecimal()}元"
                    history_reward1.text = "${obj.optNotNull("bonusPerson").formatDecimal()}元"
                    history_reward2.text = "${obj.optNotNull("bonusTeam").formatDecimal()}元"
                }
            )
    }

}
